package com.qin.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.mall.domain.OmsOrderReturnApply;
import com.qin.mall.dto.OmsReturnApplyQueryParam;
import com.qin.mall.dto.OmsUpdateStatusParam;
import com.qin.mall.dao.OmsOrderReturnApplyDao;
import com.qin.mall.dto.OmsOrderReturnApplyResult;
import com.qin.mall.service.OmsOrderReturnApplyService;
import com.qin.mall.mapper.OmsOrderReturnApplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 覃剑
* @description 针对表【oms_order_return_apply(订单退货申请)】的数据库操作Service实现
* @createDate 2022-11-10 14:38:13
*/
@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply>
    implements OmsOrderReturnApplyService{
    
    @Autowired
    private OmsOrderReturnApplyMapper returnApplyMapper;
    @Autowired
    private OmsOrderReturnApplyDao orderReturnApplyDao;

    /**
     * 分页查询申请
     * @param queryParam
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public Page<OmsOrderReturnApply> list(OmsReturnApplyQueryParam queryParam,
                                          Integer pageNum , Integer pageSize) {
        Page<OmsOrderReturnApply> page = new Page<>(pageNum, pageSize);
        QueryWrapper<OmsOrderReturnApply> wrapper = new QueryWrapper<>();
        if (queryParam.getId()!=null){
            wrapper.eq("id",queryParam.getId());
        }
        if (queryParam.getStatus()!=null){
            wrapper.eq("status",queryParam.getStatus());
        }
        if (queryParam.getHandleMan()!=null&& !Objects.equals(queryParam.getHandleMan(), "")){
            wrapper.eq("handle_man",queryParam.getHandleMan());
        }
        if (queryParam.getCreateTime()!=null&& !Objects.equals(queryParam.getCreateTime(), "")){
            wrapper.like("create_time",queryParam.getCreateTime()+"%");
        }
        if (queryParam.getHandleTime()!=null&& !Objects.equals(queryParam.getHandleTime(), "")){
            wrapper.like("handle_time",queryParam.getHandleTime()+"%");
        }
        if (queryParam.getReceiverKeyword()!=null&& !Objects.equals(queryParam.getReceiverKeyword(), "")){
            wrapper.and(i->i.like("return_name","%"+queryParam.getReceiverKeyword()+"%")
                    .or()
                    .like("return_phone","%"+queryParam.getReceiverKeyword()+"%"));
        }
        Page<OmsOrderReturnApply> pages = returnApplyMapper.selectPage(page, wrapper);
        List<OmsOrderReturnApply> records = pages.getRecords();
        return pages;
    }

    /**
     * 批量删除申请
     * @param ids
     * @return
     */
    @Override
    public int delete(List<Long> ids) {
        QueryWrapper<OmsOrderReturnApply> wrapper = new QueryWrapper<>();
        wrapper.eq("status",3)
                .in("id",ids);
        int result = returnApplyMapper.delete(wrapper);
        return result;
    }

    /**
     * 修改申请状态
     * @param id
     * @param statusParam
     * @return
     */
    @Override
    public int updateStatus(Long id, OmsUpdateStatusParam statusParam) {
        Integer status = statusParam.getStatus();
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        if(status.equals(1)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(1);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else if(status.equals(2)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(2);
            returnApply.setReceiveTime(new Date());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else if(status.equals(3)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(3);
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else{
            return 0;
        }
        int result = returnApplyMapper.updateById(returnApply);
        return result;
    }

    /**
     * 获取指定申请详情
     * @param id
     * @return
     */
    @Override
    public OmsOrderReturnApplyResult getItem(Long id) {
        OmsOrderReturnApplyResult detail = orderReturnApplyDao.getDetail(id);
        return detail;
    }
}




